<!-- 题库编辑弹窗 -->
<template>
  <el-dialog width="750px" :visible="visible" :lock-scroll="false" :destroy-on-close="true" custom-class="ele-dialog-form"
    :title="isUpdate ? '导入考生' : '导入考生'" @update:visible="updateVisible">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="考生状态:" prop="status" label-width="100px">
          <el-radio-group v-model="form.status" size="mini">
            <!-- 状态：1 为 启用，2 停用 出库 3为 停用-->
            <el-radio-button :label="1">启用</el-radio-button>
            <el-radio-button :label="2">停用</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="导入模板:">
          <el-upload ref="uploadFile" v-loading="fileLoading" class="upload-demo" element-loading-text="拼命加载中"
            element-loading-spinner="el-icon-loading" element-loading-background="rgba(0, 0, 0, 0.8)"
            :auto-upload="false" :file-list="fileList" with-credentials action="#" :limit="1" drag
            :on-change="uploadChange" :http-request="submitProData" show-file-list @submit="submitProData">
            <i class="el-icon-upload" />
            <div class="el-upload__text">
              将文件拖到此处，或
              <em>点击上传</em>
            </div>
            <div slot="tip" class="el-upload__tip" style="margin-left: 196px">仅支持上传xslx文件</div>
          </el-upload>
          <el-button type="text" icon="el-icon-download" @click="handleDownload">考试考生模版下载</el-button>
        </el-form-item>
      </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消
      </el-button>
      <el-button type="primary" :loading="loading" @click="submitProData">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import uploadImage from '@/components/uploadImage'
export default {
  name: 'memberImport',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: { uploadImage },
  data() {
    return {
      fileLoading: false, // 文件上传loading
      fileList: [],
      // 表单数据
      form: Object.assign({ status: 1, type: 1 }, this.data),
      // 表单验证规则
      rules: {
        status: [
          { required: true, message: '请选择状态', trigger: 'blur' }
        ],
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
    };
  },
  watch: {
    data() {
    }
  },
  mounted() {
  },
  // 页面创建时
  created() {
  },
  methods: {
    // 下载模版
    handleDownload() {
      window.location.href = '../excel/考生信息模板.xlsx'
    },
    // 上传到本地
    uploadChange(file) {
      this.form.file = file.raw
    },
    // 导入考试考生表格
    submitProData(file) {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.file) {
            const formdata = new FormData()
            formdata.append('file', this.form.file)
            formdata.append('status', this.form.status)
            this.$http.post('/member/importStudent', formdata).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message({ type: 'success', message: res.data.msg });
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
          } else {
            this.$message.error('文件内容不能为空');
          }
        } else {
          this.$message.error('必填项目不能为空！！');
          return false
        }
      })
      return
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },
  }
}
</script>

<style scoped></style>
